package com.ohos.carbon.slice;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.model.DataBean;
import com.ohos.carbon.slice.demos.*;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ElementScatter;

import java.util.ArrayList;

import static com.ohos.carbon.slice.MainAbilitySlice.preferences;
public class DemosSlice extends AbilitySlice {

    private Image back_button;
    private Text text_title, text;
    private ListContainer listContainer;
    private ArrayList<DataBean> mList;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_demos);
        initData();
        initView();
    }

    private void initData() {
        mList = new ArrayList<>();
        Element element = ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_warning_black_24dp);
        Element power = ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_power_settings_new_black_24dp);
        Element starFavour = ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_star_gray_24dp);
        Element favour = ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_star_border_black_24dp);
        Element search = ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_search);
        Element player = ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_play_arrow_black_24dp);
        Element parse1 = ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_share_black_24dp);
        Element person = ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_person_black_24dp);
        mList.add(new DataBean(null, "The most popular problem solutions and other fun ideas implemented with Carbon's widgets and features", null, null));
        mList.add(new DataBean(power, "Power Menu", null, preferences.getBoolean("Power Menu", false) ? starFavour : favour));
        mList.add(new DataBean(null, "Auto Complete", element, preferences.getBoolean("Auto Complete", false) ? starFavour : favour));
        mList.add(new DataBean(null, "Quick Return", null, preferences.getBoolean("Quick Return", false) ? starFavour : favour));
        mList.add(new DataBean(search, "Search Toolbar", null, preferences.getBoolean("Search Toolbar", false) ? starFavour : favour));
        mList.add(new DataBean(parse1, "Share toolbar", element, preferences.getBoolean("Share toolbar", false) ? starFavour : favour));
        mList.add(new DataBean(person, "Profile", null, preferences.getBoolean("Profile", false) ? starFavour : favour));
        mList.add(new DataBean(player, "Music player", element, preferences.getBoolean("Music player", false) ? starFavour : favour));
        mList.add(new DataBean(null, "Collapsing toolbar", null, preferences.getBoolean("Collapsing toolbar", false) ? starFavour : favour));


    }

    private void initView() {
        back_button = (Image) findComponentById(ResourceTable.Id_back_button);
        text_title = (Text) findComponentById(ResourceTable.Id_title_text);
        text_title.setText("Demos");
//        text = (Text) findComponentById(ResourceTable.Id_text);
//        text.setText(ResourceTable.String_text);
        listContainer = (ListContainer) findComponentById(ResourceTable.Id_listview);
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
        listContainer.setItemProvider(new WhatsProvider(mList, getContext()));
        listContainer.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
                switch (i) {
                    case 1:
                        Intent intent = new Intent();
                        present(new PowerMenuSlice(), intent);
                        break;
                    case 2:
                        Intent intent1 = new Intent();
                        present(new AutoCompleteSlice(), intent1);
                        break;
                    case 3:
                        Intent intent2 = new Intent();
                        present(new QuickReturnSlice(), intent2);
                        break;
                    case 4:
                        Intent intent3 = new Intent();
                        present(new SearchToolbarSlice(), intent3);
                        break;
                    case 5:
                        Intent intent4 = new Intent();
                        present(new ShareToolbarSlice(), intent4);
                        break;
                    case 6:
                        Intent intent5 = new Intent();
                        present(new ProfileSlice(), intent5);
                        break;
                    case 7:
                        Intent intent6 = new Intent();
                        present(new MusicplayerSlice(), intent6);
                        break;
                    case 8:
                        Intent intent7 = new Intent();
                        present(new CollapsingtoolbarSlice(), intent7);
                        break;
                }
            }
        });

        //返回上个页面
        back_button.setClickedListener(component -> {
            onBackPressed();
        });

    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
